﻿#pragma warning disable 1591

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace TotalFreedomBST
{
    public class BstSection : IDisposable
    {
        private readonly ViewContext _viewContext;
        private bool _disposed;
        private string _endHtml;

        public string Id { get; set; }

        public BstSection(ViewContext viewContext, string endHtml = null)
        {
            _endHtml = endHtml;

            if (viewContext == null)
            {
                throw new ArgumentNullException("viewContext");
            }

            _viewContext = viewContext;            
        }

        public virtual string StartHtml()
        {
            return "";
        }

        protected virtual string EndHtml()
        {
            return "";
        }

        public void Dispose()
        {
            Dispose(true /* disposing */);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                _disposed = true;
                EndBstSection();
            }
        }

        public void EndSection()
        {
            Dispose(true);
        }

        private void EndBstSection()
        {
            string s = EndHtml();
            _viewContext.Writer.Write(s + _endHtml);
            _viewContext.OutputClientValidation();
            _viewContext.FormContext = null;
        }
    }
}